#coding=utf-8
import stutils

def get_ipo_from_soup(soup):
    t = unicode('上市日期', 'u8')
    
    rq = soup.find(text=lambda x: x.find(t) !=  -1)
    if not rq:return None
    
    date = rq.parent.parent.nextSibling.nextSibling.string
    date = date.encode('u8')
    return date

class NoIpoDateError(Exception):
    def __str__(self,code, site):
        return "Can not find ipo date from %s of %s"%(code, site)
    
def get_ipo(code):
    '''获得上市公司的上市日期'''
    url = "http://stock.business.sohu.com/q/gsjs.php?&code=%s"%code
    
    s = stutils.get_soup(url)
    date = get_ipo_from_soup(s)
    if not date:
        raise NoIpoDateError(code, 'sohu')
    return date
